//
//  PromisePopViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/8.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "PromisePopCell.h"

#import "PromisePopViewController.h"

@interface PromisePopViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@property (strong, nonatomic) NSMutableArray <CommonPromiseModel *>*dataArray;

@property (strong, nonatomic) NSArray *data;

@end

@implementation PromisePopViewController

- (instancetype)initDefaultHeight:(CGFloat)height vc:(UIViewController *)vc dataSource:(NSMutableArray <CommonPromiseModel *>*)dataSource {
//    self.dataArray = @[].mutableCopy;
    if (self = [super init]) {
//        self.vc = vc;
        self.dataArray = @[].mutableCopy;
        self.dataArray = dataSource.mutableCopy;
//        self.defaultHeight = height;
//        self.type = type;
    }
    return self;
}

- (void)prepareData {
    
    for (NSDictionary *dic in self.data) {
        CommonPromiseModel *model = [CommonPromiseModel modelWithDictionary:dic];
        model.itemHeight = [Utilities getStringHeightWithText:model.subTitle font:[UIFont systemFontOfSize:12] viewWidth:(TTScreenWidth - 50)] + 40;
        
//        [Utilities heightOfString:model.subTitle font:[UIFont systemFontOfSize:12] width:(TTScreenWidth - 50) maxHeight:100] + 40;
        [self.dataArray addObject:model];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initPop];
    [self prepareData];
    [self initView];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.view pp_clipCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:10];
    });
}

- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.contentSizeInPopup = CGSizeMake(TTScreenWidth, TTScreenHeight*0.8);
    self.popupController.navigationBarHidden = YES;
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)]];
}

- (void)initView {
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(50);
        make.bottom.mas_equalTo(-60);
    }];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.font = [UIFont boldSystemFontOfSize:16];
    lab.textColor = UIColorHex(0x222222);
    lab.text = @"婚前婚后承诺";
    [self.view addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(self.tableView.mas_top);
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn setTitleColor:UIColorHex(0xffffff) forState:UIControlStateNormal];
    [btn setBackgroundColor:UIColorHex(0xff5539)];
    btn.layer.cornerRadius = 20.0f;
    [btn addTarget:self action:@selector(backgroundTap) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.left.mas_equalTo(14);
        make.top.mas_equalTo(self.tableView.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    [self.tableView reloadData];
}

- (void)backgroundTap  {
    [self.popupController dismiss];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    PromisePopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PromisePopCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.dataArray[indexPath.row].itemHeight;
}

- (NSArray *)data {
    NSArray *data = @[
        @{@"img": @"zhuanshuguwen", @"title": @"专属顾问", @"subTitle": @"平台提供专属顾问为您备婚选择提供免费咨询服务，不推荐任何商家任何套餐。"},
        @{@"img": @"icon_dingdanlijian", @"title": @"订单立减", @"subTitle": @"在平台所有商户订单，成交价基础上都会获得现金补贴（使用自有资金支付的）。"},
        @{@"img": @"icon_xinyongkatiexi", @"title": @"信用卡贴息", @"subTitle": @"使用平台合作银行信用卡支付享受全额免息免手续费自动分期。"},
        @{@"img": @"icon_beiyongjin", @"title": @"结婚备用金", @"subTitle": @"可在平台申请结婚备用金，由合作国有/大型正规银行提供，审批通过可享受免息免手续费自动分期。"},
        @{@"img": @"icon_falvzixun", @"title": @"法律咨询", @"subTitle": @"为您提供免费法律咨询服务，购买服务产品后出现售后问题也可免费咨询。"},
        @{@"img": @"icon_qingganzixun", @"title": @"情感/心理咨询", @"subTitle": @"为您提供免费情感/心理咨询服务，情感关系，婆媳关系等。"},
    ];
    return data;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor =  UIColorHex(0xffffff);

        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedRowHeight = 0;
        
        [_tableView registerNib:[UINib nibWithNibName:@"PromisePopCell" bundle:nil] forCellReuseIdentifier:@"PromisePopCellID"];
    }
    return _tableView;
}

@end
